import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/show")
public class ShowRequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 调用 上述 api , 把得到的结果构造成一个大字符串,统一返回客户端
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(req.getProtocol());
        System.out.println("<br>"); // br 标签表示换行
        stringBuilder.append(req.getMethod());
        System.out.println("<br>");
        stringBuilder.append(req.getRequestURI());
        System.out.println("<br>");
        stringBuilder.append(req.getContextPath());
        System.out.println("<br>");
        stringBuilder.append(req.getQueryString());
        System.out.println("<br>");
        // 获取所有的 header
        Enumeration<String> headerNames = req.getHeaderNames();  // 这是一个枚举类型
        while(headerNames.hasMoreElements()){
            String key = headerNames.nextElement();
            String value = req.getHeader(key);
            stringBuilder.append(key + " : "+ value+"<br>" );
        }
        // 告诉浏览器的数据类型, 任何一次服务器返回, 都应该做的事情
        resp.setContentType("text/html; charset=utf8");
        // 把上述内容整体返回到客户端这边
        resp.getWriter().write(stringBuilder.toString()); // 智能返回字符串

    }
}
